if (FALSE) {
### Save code in a file "demo.r" and run with 2 processors by
### SHELL> mpiexec -np 2 Rscript demo.r
spmd.code <- "
### Initialize
suppressMessages(library(pbdMPI, quietly = TRUE))
### Examples.
comm.set.seed(123456, diff = FALSE)
X <- matrix(rnorm(10), ncol = 2)
id.matrix <- comm.allpairs(nrow(X))
### Method original.
dist.org <- dist(X)
### Method 1.
dist.common <- comm.pairwise(X, pairid.gbd = id.matrix)
### Method 2.
# if(comm.rank() != 0){
# X <- matrix(0, nrow = 0, ncol = 4)
# }
X.gbd <- comm.as.gbd(X) ### The other way.
dist.gbd <- comm.pairwise(X.gbd)
### Verify.
d.org <- as.vector(dist.org)
d.1 <- do.call(\"c\", allgather(dist.common[, 3]))
d.2 <- do.call(\"c\", allgather(dist.gbd[, 3]))
comm.print(all(d.org == d.1))
comm.print(all(d.org == d.2))
### Finish.
finalize()
"
# execmpi(spmd.code, nranks = 2L)
}
Run the code above in your browser using DataLab